<?php
/**
 * Created by PhpStorm.
 * User: 杨海
 * Date: 2021/3/23
 * Time: 9:44
 */

namespace app\index\controller;


use think\Controller;
use think\facade\Cache;
use think\facade\Cookie;
use think\facade\Session;
use think\Request;

class Banner extends  Common
{
    /**
     * 轮播图展示页面
     * @return mixed|\think\response\Json
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $model = new \app\index\model\Banner();

            $arrayKey = $model->column("article_id");
            $page   = $this->request->param('page/d');
            $limit  = $this->request->param('limit/d');

            $data['data']   = $model->table("article")->whereIn("id",$arrayKey)->page($page)->limit($limit)->order('create_time')->select();
            $data['count']  = $model->table("article")->whereIn("id",$arrayKey)->count('id');
            $data['code']   = 0;
            return json($data);
        }

        $userInfo = Session::get("userClass" . Cookie::get("userId"));
        $this->assign("userInfo",$userInfo);
        return $this->fetch();
    }

    /**
     * 添加页面不在轮播中的数据接口
     * @return \think\response\Json
     */
    public function queryArticle()
    {
        if ($this->request->isAjax()) {
            $model = new \app\index\model\Banner();

            $arrayKey = $model->column("article_id");
            $page = $this->request->param('page/d', 1);
            $limit = $this->request->param('limit/d', 2);


            $data['data'] = $model->table("article")->whereNotIn("id", $arrayKey)->page($page)->limit($limit)->order('create_time')->select();
            $data['count'] = $model->table("article")->whereNotIn("id", $arrayKey)->count('id');
            $data['code'] = 0;

            return json($data);
        }
    }

    /**
     * 添加文章到轮播图
     * @return mixed|\think\response\Json
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $model = new \app\index\model\Banner();
            $key = $this->request->param();
            $result = $model->save(["article_id" => $key["id"]]);
            if (!$result) {
                return json(["code" => 0,"msg" => "添加失败"]);
            }
            alter_redis();
            return json(["code" => 1,"msg" => "添加成功"]);
        }
        return $this->fetch();
    }

    /**
     * 取消正在轮播中的文章
     * @return \think\response\Json
     * @throws \Exception
     */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $model = new \app\index\model\Banner();
            $key = $this->request->param();
            $result = $model->where("article_id",$key["id"])->delete();
            if (!$result) {
                return json(["code" => 0,"msg" => "取消轮播失败"]);
            }
            alter_redis();
            return json(["code" => 1,"msg" => "文章已取消轮播"]);
        }
    }
}

function alter_redis() {
    $mark = unserialize(Cache::get("article_list"));
    Cache::set("article_list",$mark,1);
    $banner = unserialize(Cache::get("banner"));
    Cache::set("banner",serialize($banner),1);
}